﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BarberShop.Model;
using BarberShop.Data;
using System.Linq.Expressions;
using BarberShop.Common;

namespace BarberShop.BLL
{
    public class AdImgPalyListBLL
    {

        public static IList<AdPlayListInfo> GetAdPlayListInfo(string name)
        { 
            using(DataClassesDataContext data = new DataClassesDataContext())
            {
                Expression<Func<AdPlayListInfo, bool>> WhereExpr = PredicateExtensions.True<AdPlayListInfo>();
                if (!string.IsNullOrEmpty(name))
                {
                    WhereExpr = WhereExpr.And(x => x.Name.Contains(name));
                }
                var AdList = data.AdPlayListInfo.Where(WhereExpr);
                return AdList.ToList();
            }
        }

        public static IList<View_AdPlayList> GetViewAdPalyList(int id)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var list = data.View_AdPlayList.Where(r => r.PlayListId == id);
                return list.OrderBy(r => r.PlayIndex).ToList();
            }
        }
        /// <summary>
        /// 添加广告播放列表
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool AddAdPlayListInfo(AdPlayListInfo info)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    data.AdPlayListInfo.InsertOnSubmit(info);
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }

        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static AdPlayListInfo GetAdPlayListInfo(int id)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var info = (from a in data.AdPlayListInfo where a.ID == id select a).FirstOrDefault();
                return info;
            }
        }

        public static bool UpdateAdPlayListInfo(AdPlayListInfo info)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    var r = (from a in data.AdPlayListInfo where a.ID == info.ID select a).FirstOrDefault();
                    r.CreateDate = info.CreateDate;
                    r.ID = info.ID;
                    r.Name = info.Name;
                    r.Status = info.Status;
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }
        }
    }
}
